package com.wxtf.platform.main.report;

import java.util.*;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wxtf.platform.interfaces.CommonsCallProvider;
import com.wxtf.platform.plugins.user.UserBean;
import com.wxtf.platform.supers.LogicBase;
import com.wxtf.platform.tools.*;

/**
 * <li>Title: ChartAction.java</li>
 * <li>Project: baseplatform</li>
 * <li>Package: com.wxtf.platform.main.report</li>
 * <li>Description: 图形报表</li>
 * <li>Copyright: Copyright (c) 2012</li>
 * <li>Company: wxthtf Technologies </li>
 * <li>Created on Sep 26, 2012 9:19:20 AM</li>
 * 
 * @author chun_chang
 * @version 1.0.0.0
 * 
 */
public class ChartAction extends LogicBase implements CommonsCallProvider{
	private static final Log log = LogFactory.getLog(ChartAction.class);
	
	public ChartAction(String jsonArray){

	}

	@Override
	public Object run() {
		Object msg = "";
		try {
			if("chart1".equals(this.getMethodName())) {//生育条件图表
				msg = this.select(this.getLogicData());
			} else if("chart2".equals(this.getMethodName())) {//生育情况图表
				msg = this.insert(this.getLogicData());
			} else if("chart3".equals(this.getMethodName())) {//独生子女
				msg = this.update(this.getLogicData());
			} else if("chart4".equals(this.getMethodName())) {//四术情况
				msg = this.delete(this.getLogicData());
			} else if("chart5".equals(this.getMethodName())) {//四术情况
				msg = this.updateBatch(this.getLogicData());
			} else if("chart6".equals(this.getMethodName())) {//四术情况
				msg = this.recordCount(this.getLogicData());
			} else {
				msg = "\"没有【" + this.getMethodName() + "】该方法\"";
			}

			if(msg == null || "[null]".equals(msg)) {
				msg = "\"\"";
			}
			if(msg.toString().startsWith("{") || msg.toString().startsWith("[")) {
				return msg;
			}
			msg = ConstantInfoHelper.generateJSONString(msg.toString());
		} catch(Exception e) {
			msg = ConstantInfoHelper.generateErrorJSONString();
			log.error(e, e);
		} finally {
			this.clear();
		}
		return msg.toString();
	}

	@Override
	public Object select(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		JSONArray array = JSONArray.fromObject(jsonArray);
		if(array.size() > 0) {
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			String cons = jsonObject.getString("cons");
			String year = jsonObject.getString("year");
			String quarter = jsonObject.getString("quarter");
			
			int mCnt0 = 0;//可生育-男
			int fCnt0 = 0;//可生育-女
			int mCnt1 = 0;//无生育-男
			int fCnt1 = 0;//无生育-女
			
			sql = "select e_yhyl_ysy_nan,e_yhyl_ysy_nv,e_yhyl_wsy_nan,e_yhyl_wsy_nv from tb_rkyjhsy_baobiao1 where org_id = ? and bb_year = ? and bb_quarter = ?";
			String[] jsonField = {
					"mCnt0", "fCnt0", "mCnt1", "fCnt1"
			};
			Object[] params = { user.getCurOrgId(), year, quarter };
			List<Map<String, Object>> list1 = AccessDataBaseUtil.query4ListMap(jsonField, sql, params);
			if(null != list1 && list1.size() > 0) {
				Map<String, Object> map = list1.get(0);
				mCnt0 = Integer.valueOf(String.valueOf(map.get("mCnt0") == null ? 0 : map.get("mCnt0")));
				fCnt0 = Integer.valueOf(String.valueOf(map.get("fCnt0") == null ? 0 : map.get("fCnt0")));
				mCnt1 = Integer.valueOf(String.valueOf(map.get("mCnt1") == null ? 0 : map.get("mCnt1")));
				fCnt1 = Integer.valueOf(String.valueOf(map.get("fCnt1") == null ? 0 : map.get("fCnt1")));
			}
			
			StringBuffer buff = new StringBuffer();
			buff.append("[");
			if(cons.indexOf("con1") > -1){
				buff.append("{\"key\":\"有生育条件-男\",\"value\":\"").append(mCnt0).append("\",\"color\": \"#FF0000\"}").append(",");
			}
			if(cons.indexOf("con2") > -1) {
				buff.append("{\"key\":\"有生育条件-女\",\"value\":\"").append(fCnt0).append("\",\"color\": \"#00FF00\"}").append(",");
			}
			if(cons.indexOf("con3") > -1) {
				buff.append("{\"key\":\"无生育条件-男\",\"value\":\"").append(mCnt1).append("\",\"color\": \"#0000FF\"}").append(",");
			}
			if(cons.indexOf("con4") > -1) {
				buff.append("{\"key\":\"无生育条件-女\",\"value\":\"").append(fCnt1).append("\",\"color\": \"FFFF00\"}").append(",");
			}
			if(buff.length() > 1){
				buff.deleteCharAt(buff.length() - 1);
			}
			
			buff.append("]");
			return buff.toString();
		}
    	return null;
	}

	@Override
	public Object insert(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		JSONArray array = JSONArray.fromObject(jsonArray);
		if(array.size() > 0) {
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			String cons = jsonObject.getString("cons");
			String year = jsonObject.getString("year");
			String quarter = jsonObject.getString("quarter");
			
			int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0, cnt6 = 0;
			sql = "select e_syqk_csyes,e_syqk_qz_jhnyt,e_syqk_qz_jhnet,e_syqk_qz_wyrs,e_syqk_qz_hyrs from tb_rkyjhsy_baobiao2 where org_id = ? and bb_year = ? and bb_quarter = ?";
			String[] jsonField = {
					"con5", "con3", "con4", "con6", "con1"
			};
			Object[] params = { user.getCurOrgId(), year, quarter };
			List<Map<String, Object>> list1 = AccessDataBaseUtil.query4ListMap(jsonField, sql, params);
			if(null != list1 && list1.size() > 0) {
				Map<String, Object> map = list1.get(0);
				cnt1 = Integer.valueOf(String.valueOf(map.get("con1") == null ? 0 : map.get("con1")));
				cnt3 = Integer.valueOf(String.valueOf(map.get("con3") == null ? 0 : map.get("con3")));
				cnt4 = Integer.valueOf(String.valueOf(map.get("con4") == null ? 0 : map.get("con4")));
				cnt5 = Integer.valueOf(String.valueOf(map.get("con5") == null ? 0 : map.get("con5")));
				cnt6 = Integer.valueOf(String.valueOf(map.get("con6") == null ? 0 : map.get("con6")));
			}
			
			StringBuffer buff = new StringBuffer();
			buff.append("[");
			if(cons.indexOf("con1") > -1){
				buff.append("{\"key\":\"怀孕人数\",\"value\":\"").append(cnt1).append("\",\"color\": \"#FF0F00\"},");
			}
			if(cons.indexOf("con2") > -1) {
				sql = "select e_dyrs from tb_rkyjhsy_baobiao1 where org_id = ? and bb_type = '1' and bb_year = ? and bb_quarter = ?";
				cnt2 = AccessDataBaseUtil.queryForInt(sql, params);
				buff.append("{\"key\":\"等孕人数\",\"value\":\"").append(cnt2).append("\",\"color\": \"#FF6600\"},");
			}
			if(cons.indexOf("con3") > -1) {
				buff.append("{\"key\":\"计划内一胎\",\"value\":\"").append(cnt3).append("\",\"color\": \"#FF9E01\"},");
			}
			if(cons.indexOf("con4") > -1) {
				buff.append("{\"key\":\"计划内二胎\",\"value\":\"").append(cnt4).append("\",\"color\": \"#FCD202\"},");
			}
			if(cons.indexOf("con5") > -1) {
				buff.append("{\"key\":\"出生婴儿\",\"value\":\"").append(cnt5).append("\",\"color\": \"#F8FF01\"},");
			}
			if(cons.indexOf("con6") > -1) {
				buff.append("{\"key\":\"晚婚+晚育\",\"value\":\"").append(cnt6).append("\",\"color\": \"#B0DE09\"},");
			}
			if(buff.length() > 1){
				buff.deleteCharAt(buff.length() - 1);
			}
			buff.append("]");
			return buff.toString();
		}
		return null;
	}

	@Override
	public Object update(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		JSONArray array = JSONArray.fromObject(jsonArray);
		if(array.size() > 0) {
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			String cons = jsonObject.getString("cons");
			String year = jsonObject.getString("year");
			String quarter = jsonObject.getString("quarter");
			
			int cnt1 = 0, cnt2 = 0, cnt3 = 0;
			sql = "select e_dsznqk_yingbanzheng,e_dsznqk_yibanzheng,e_dsznqk_xydszns from tb_rkyjhsy_baobiao2 where org_id = ? and bb_year = ? and bb_quarter = ?";
			String[] jsonField = { "con1", "con2", "con3" };
			Object[] params = { user.getCurOrgId(), year, quarter };
			List<Map<String, Object>> list1 = AccessDataBaseUtil.query4ListMap(jsonField, sql, params);
			if(null != list1 && list1.size() > 0) {
				Map<String, Object> map = list1.get(0);
				cnt1 = Integer.valueOf(String.valueOf(map.get("con1") == null ? 0 : map.get("con1")));
				cnt2 = Integer.valueOf(String.valueOf(map.get("con2") == null ? 0 : map.get("con2")));
				cnt3 = Integer.valueOf(String.valueOf(map.get("con3") == null ? 0 : map.get("con3")));
			}
			
			StringBuffer buff = new StringBuffer();
			buff.append("[");
			if(cons.indexOf("con1") > -1){
				buff.append("{\"key\":\"应办证职工数\",\"value\":\"").append(cnt1).append("\",\"color\": \"#FF0F00\"},");
			}
			if(cons.indexOf("con2") > -1) {
				buff.append("{\"key\":\"已办证职工数\",\"value\":\"").append(cnt2).append("\",\"color\": \"#FF6600\"},");
			}
			if(cons.indexOf("con3") > -1) {
				buff.append("{\"key\":\"现有独生子女数\",\"value\":\"").append(cnt3).append("\",\"color\": \"#FF9E01\"},");
			}
			if(buff.length() > 1){
				buff.deleteCharAt(buff.length() - 1);
			}
			buff.append("]");
			return buff.toString();
		}
		return null;
	}

	@Override
	public Object delete(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		JSONArray array = JSONArray.fromObject(jsonArray);
		if(array.size() > 0) {
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			String cons = jsonObject.getString("cons");
			String year = jsonObject.getString("year");
			String quarter = jsonObject.getString("quarter");
			
			int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0, cnt6 = 0, cnt7 = 0;
			sql = "select e_ylsjycs_yls_jz,e_ylsjycs_yls_sh,e_ylsjycs_yls_pm,e_ylsjycs_yls_gj,e_ylsjycs_yls_ym,e_ylsjycs_yls_fy,e_ylsjycs_yls_qt from tb_rkyjhsy_baobiao1 where org_id = ? and bb_year = ? and bb_quarter = ?";
			String[] jsonField = { "con1", "con2", "con3", "con4", "con5", "con6", "con7" };
			Object[] params = { user.getCurOrgId(), year, quarter };
			List<Map<String, Object>> list1 = AccessDataBaseUtil.query4ListMap(jsonField, sql, params);
			if(null != list1 && list1.size() > 0) {
				Map<String, Object> map = list1.get(0);
				cnt1 = Integer.valueOf(String.valueOf(map.get("con1") == null ? 0 : map.get("con1")));
				cnt2 = Integer.valueOf(String.valueOf(map.get("con2") == null ? 0 : map.get("con2")));
				cnt3 = Integer.valueOf(String.valueOf(map.get("con3") == null ? 0 : map.get("con3")));
				cnt4 = Integer.valueOf(String.valueOf(map.get("con4") == null ? 0 : map.get("con4")));
				cnt5 = Integer.valueOf(String.valueOf(map.get("con5") == null ? 0 : map.get("con5")));
				cnt6 = Integer.valueOf(String.valueOf(map.get("con6") == null ? 0 : map.get("con6")));
				cnt7 = Integer.valueOf(String.valueOf(map.get("con7") == null ? 0 : map.get("con7")));
			}
			
			StringBuffer buff = new StringBuffer();
			buff.append("[");
			if(cons.indexOf("con1") > -1){
				buff.append("{\"key\":\"结扎\",\"value\":\"").append(cnt1).append("\",\"color\": \"#FF0F00\"},");
			}
			if(cons.indexOf("con2") > -1) {
				buff.append("{\"key\":\"上环\",\"value\":\"").append(cnt2).append("\",\"color\": \"#FF6600\"},");
			}
			if(cons.indexOf("con3") > -1) {
				buff.append("{\"key\":\"皮埋\",\"value\":\"").append(cnt3).append("\",\"color\": \"#FF9E01\"},");
			}
			if(cons.indexOf("con4") > -1) {
				buff.append("{\"key\":\"工具\",\"value\":\"").append(cnt4).append("\",\"color\": \"#FCD202\"},");
			}
			if(cons.indexOf("con5") > -1) {
				buff.append("{\"key\":\"药膜\",\"value\":\"").append(cnt5).append("\",\"color\": \"#F8FF01\"},");
			}
			if(cons.indexOf("con6") > -1) {
				buff.append("{\"key\":\"服药\",\"value\":\"").append(cnt6).append("\",\"color\": \"#B0DE09\"},");
			}
			if(cons.indexOf("con7") > -1) {
				buff.append("{\"key\":\"其他\",\"value\":\"").append(cnt7).append("\",\"color\": \"#04D215\"},");
			}
			if(buff.length() > 1){
				buff.deleteCharAt(buff.length() - 1);
			}
			buff.append("]");
			return buff.toString();
		}
		return null;
	}
	
	@Override
	public Object updateBatch(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		JSONArray array = JSONArray.fromObject(jsonArray);
		if(array.size() > 0) {
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			String cons = jsonObject.getString("cons");
			String year = jsonObject.getString("year");
			String quarter = jsonObject.getString("quarter");
			
			int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0;
			sql = "select e_ssqk_rl,e_ssqk_sh,e_ssqk_jz,e_ssqk_yc_zq,e_ssqk_yc_dyf from tb_rkyjhsy_baobiao2 where org_id = ? and bb_year = ? and bb_quarter = ?";
			String[] jsonField = { "con1", "con2", "con3", "con4", "con5" };
			Object[] params = { user.getCurOrgId(), year, quarter };
			List<Map<String, Object>> list1 = AccessDataBaseUtil.query4ListMap(jsonField, sql, params);
			if(null != list1 && list1.size() > 0) {
				Map<String, Object> map = list1.get(0);
				cnt1 = Integer.valueOf(String.valueOf(map.get("con1") == null ? 0 : map.get("con1")));
				cnt2 = Integer.valueOf(String.valueOf(map.get("con2") == null ? 0 : map.get("con2")));
				cnt3 = Integer.valueOf(String.valueOf(map.get("con3") == null ? 0 : map.get("con3")));
				cnt4 = Integer.valueOf(String.valueOf(map.get("con4") == null ? 0 : map.get("con4")));
				cnt5 = Integer.valueOf(String.valueOf(map.get("con5") == null ? 0 : map.get("con5")));
			}
			
			StringBuffer buff = new StringBuffer();
			buff.append("[");
			if(cons.indexOf("con1") > -1){
				buff.append("{\"key\":\"人流\",\"value\":\"").append(cnt1).append("\",\"color\": \"#FF0F00\"},");
			}
			if(cons.indexOf("con2") > -1) {
				buff.append("{\"key\":\"上环\",\"value\":\"").append(cnt2).append("\",\"color\": \"#FF6600\"},");
			}
			if(cons.indexOf("con3") > -1) {
				buff.append("{\"key\":\"结扎\",\"value\":\"").append(cnt3).append("\",\"color\": \"#FF9E01\"},");
			}
			if(cons.indexOf("con4") > -1) {
				buff.append("{\"key\":\"引产-中期\",\"value\":\"").append(cnt4).append("\",\"color\": \"#FF9E01\"},");
			}
			if(cons.indexOf("con5") > -1) {
				buff.append("{\"key\":\"引产-大月份\",\"value\":\"").append(cnt5).append("\",\"color\": \"#FF9E01\"},");
			}
			if(buff.length() > 1){
				buff.deleteCharAt(buff.length() - 1);
			}
			buff.append("]");
			return buff.toString();
		}
		return null;
	}

	@Override
	public Object recordCount(Object jsonArray) throws Exception {
		UserBean user = (UserBean)this.getRequest().getSession().getAttribute("userBean");
		JSONArray array = JSONArray.fromObject(jsonArray);
		if(array.size() > 0) {
			JSONObject jsonObject = JSONObject.fromObject(array.getString(0));
			String year = jsonObject.getString("year");
			String quarter = jsonObject.getString("quarter");
			
			int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
			sql = "select e_hyzk_whl,e_syqk_qz_wyl,e_syqk_qz_jhsyl,e_dsznqk_bzl from tb_rkyjhsy_baobiao2 where org_id = ? and bb_year = ? and bb_quarter = ?";
			String[] jsonField = { "con1", "con2", "con3", "con4" };
			Object[] params = { user.getCurOrgId(), year, quarter };
			List<Map<String, Object>> list1 = AccessDataBaseUtil.query4ListMap(jsonField, sql, params);
			if(null != list1 && list1.size() > 0) {
				Map<String, Object> map = list1.get(0);
				cnt1 = Double.valueOf(String.valueOf(map.get("con1") == null ? 0 : map.get("con1"))).intValue();
				cnt2 = Double.valueOf(String.valueOf(map.get("con2") == null ? 0 : map.get("con2"))).intValue();
				cnt3 = Double.valueOf(String.valueOf(map.get("con3") == null ? 0 : map.get("con3"))).intValue();
				cnt4 = Double.valueOf(String.valueOf(map.get("con4") == null ? 0 : map.get("con4"))).intValue();
			}
			
			StringBuffer buff = new StringBuffer();
			buff.append("[");
			buff.append("{\"chart1\":[{\"key\":\"晚婚率\",\"value\":\"").append(cnt1).append("\"},{\"key\":\"非晚婚\",\"value\":\"").append(100-cnt1).append("\"}]},");
			buff.append("{\"chart2\":[{\"key\":\"晚育率\",\"value\":\"").append(cnt2).append("\"},{\"key\":\"非晚育\",\"value\":\"").append(100-cnt1).append("\"}]},");
			buff.append("{\"chart3\":[{\"key\":\"计划生育率\",\"value\":\"").append(cnt3).append("\"},{\"key\":\"计划外生育\",\"value\":\"").append(100-cnt1).append("\"}]},");
			buff.append("{\"chart4\":[{\"key\":\"办证率\",\"value\":\"").append(cnt4).append("\"},{\"key\":\"未办证\",\"value\":\"").append(100-cnt1).append("\"}]}");
			buff.append("]");
			return buff.toString();
		}
		return null;
	}

	@Override
	public Object setRequest(HttpServletRequest request) throws Exception {
		this.request = request;
		return request;
	}

	@Override
	public Object onCreate(Object jsonArray) {
		return null;
	}

	@Override
	public Object onDestory(Object jsonArray) {
		return null;
	}

	@Override
	public Object onInit(Object jsonArray) {
		return null;
	}

	
}
